package com.google.wave.api;

/**
 * Class that handle a event
 * @author Paulo Romano
 * @version 1.00
 *
 */
public class EventHandle {
	
	private SelfAddedListener[] selfAddedListeners;
	private int lastSelfAdded;
	
	private ParticipantsChangedListener[] participantsChangedListeners;
	private int lastParticipantsChanged;
	
	/**
	 * Default constructor
	 */
	public EventHandle(){
		selfAddedListeners = new SelfAddedListener[1];
		lastSelfAdded = 0;
		
		participantsChangedListeners = new ParticipantsChangedListener[1];
		lastParticipantsChanged = 0;
	}
	
	/**
	 * Handle the event
	 * @param e event to be handled
	 */
	public void processEvent(Event e){
		switch (e.getType()) {
		
		//Occurs when the robot was added to the wave
		case WAVELET_SELF_ADDED:
	        for(int i=0;i<selfAddedListeners.length-1;i++){
	        	if (selfAddedListeners[i] != null){ 
	        		selfAddedListeners[i].onSelfAdded(e);
	        	}
	        }
			break;
		
		//Occurs when the participants of the wave was changed
		case WAVELET_PARTICIPANTS_CHANGED:
			for(int i=0;i<participantsChangedListeners.length-1;i++){  
				if (participantsChangedListeners[i] != null){
					participantsChangedListeners[i].onParticipantsChanged(e);
					
				}
	        }
			break;
		default:
			break;
		}
  
	}
	
	//SelfAddedListener -------------------------------------------------------------------
	
	/**
	 * Grow-up the length of the selfAddedListeners array 
	 */
	protected void appendSelfAddedListeners(){
		SelfAddedListener[] listenersCopy = selfAddedListeners;
		selfAddedListeners = new SelfAddedListener[listenersCopy.length+1];
		for (int i = 0; i < listenersCopy.length; i++) {
			selfAddedListeners[i] = listenersCopy[i];
		}		
	}
	
	/**
	 * Assign a new SelfAddedListener to the listener array
	 * @param e the listener to be added
	 */
	public void addSelfAddedListener(SelfAddedListener e){
		this.appendSelfAddedListeners();
		selfAddedListeners[lastSelfAdded++] = e;
	}
	
	//ParticipantsChangedListner -------------------------------------------------------------------

	/**
	 * Grow-up the length of the selfAddedListeners array 
	 */
	protected void appendParticipantsChangedListeners(){
		ParticipantsChangedListener[] listenersCopy = participantsChangedListeners;
		participantsChangedListeners = new ParticipantsChangedListener[listenersCopy.length+1];
		for (int i = 0; i < listenersCopy.length-1; i++) {
			participantsChangedListeners[i] = listenersCopy[i];
		}
	}
	
	/**
	 * Assign a new ParticipantsChanged
	 * @param e the listener to be added
	 */
	public void addParticipantsChanged(ParticipantsChangedListener e){
		this.appendParticipantsChangedListeners();
		participantsChangedListeners[lastParticipantsChanged++] = e;
	}
	
	//BlipCreatedListener -------------------------------------------------------------------
	
	//TODO Implements the logic to handle with BlipCreated

}
